package com.hopes.bizz.poll.shardingsphere.strategy;

import com.hopes.bizz.common.core.util.DateUtils;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/10/13 16:48
 * @Description:
 */
public class PollMinAlgorithm implements StandardShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        return preciseShardingValue.getLogicTableName() + "_" + DateUtils.limitYear(preciseShardingValue.getValue());
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Date> rangeShardingValue) {
        List<String> result = new ArrayList<>();

        result.add(rangeShardingValue.getLogicTableName() + "_" + DateUtils.limitYear(rangeShardingValue.getValueRange().lowerEndpoint()));
        result.add(rangeShardingValue.getLogicTableName() + "_" + DateUtils.limitYear(rangeShardingValue.getValueRange().upperEndpoint()));

        return result;
    }

    @Override
    public void init() {

    }

    @Override
    public String getType() {
        return "PollMinAlgorithm";
    }
}
